﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Invoicing.Model.Products;
namespace Invoicing.Command.Products
{
    public class ProductStockCommand
    {
        public static ProductStockCollection Gets(ProductStockQuery sea)
        {
            var ret = new ProductStockCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<ProductStock> q = db.DataContext.ProductStocks;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.MaterialID > 0)
                        q = q.Where(p => p.MaterialID == sea.MaterialID);
                    if (sea.ProcessID > 0)
                        q = q.Where(p => p.ProcessID == sea.ProcessID);
                    if (!string.IsNullOrEmpty(sea.Size))
                        q = q.Where(p => p.Size == sea.Size);
                    if (sea.HouseID > 0)
                        q = q.Where(p => p.HouseID == sea.HouseID);
                }
                var n = q.Count();
                if (n > 0)
                {
                    if (sea != null && sea.OrderBy!=null)
                    {
                        switch (sea.OrderBy.Field)
                        {
                            case "Quantity":
                                if (sea.OrderBy.Dir == "asc")
                                    q = q.OrderBy(p => p.Quantity);
                                else
                                    q = q.OrderByDescending(p => p.Quantity);
                                break;
                            default:
                                q = q.OrderByDescending(p => p.ProductID);
                                break;
                        }
                    }
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<ProductStockModel, ProductStock, ProductStockCollection>(lst);
                    ret.TotalRecordsCount = n;

                    if (sea != null)
                    {
                        if (sea.GetProduct)
                        {
                            ret.ForEach(p =>
                            {
                                var d = lst.Find(o => o.ID == p.ID);
                                p.Product = Convertor.ConvertTo<ProductModel, Product>(d.Product);
                                p.MaterialName = d.ProductMaterial.MaterialName;
                                p.ProcessName = d.ProductProcess.ProcessName;

                                if (d.Product.PType == 2 && d.HouseID==1)
                                {
                                    var ratio = db.DataContext.ProductRatios.FirstOrDefault(o => o.ProductID == d.ProductID && o.MaterialID == d.MaterialID && o.Size == d.Size);
                                    if (ratio != null)
                                        p.Ratio = ratio.Ratio;
                                }
                            });
                        }
                    }
                }
            }
            return ret;
        }

        public static decimal[] GetSum(ProductStockQuery sea)
        {
            using (DBCentre db = new DBCentre())
            {
                IQueryable<ProductStock> q = db.DataContext.ProductStocks;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.MaterialID > 0)
                        q = q.Where(p => p.MaterialID == sea.MaterialID);
                    if (sea.ProcessID > 0)
                        q = q.Where(p => p.ProcessID == sea.ProcessID);
                    if (!string.IsNullOrEmpty(sea.Size))
                        q = q.Where(p => p.Size == sea.Size);
                    if (sea.HouseID > 0)
                        q = q.Where(p => p.HouseID == sea.HouseID);
                }
                var r = from st in q
                        join ra in db.DataContext.ProductRatios
                        on new { st.ProductID, st.MaterialID, st.Size }
                        equals new { ra.ProductID, ra.MaterialID, ra.Size }
                        into joinTemp
                        from temp in joinTemp.DefaultIfEmpty()
                        select new
                        {
                            Ratio = temp == null ? 0 : temp.Ratio,
                            st.Quantity
                        };
                r = r.Where(p => p.Ratio > 0);
                var n = r.Count();
                var ret = new decimal[] { 0, 0 };
                if (n > 0)
                {
                    ret[0] = r.Sum(p => p.Quantity);
                    ret[1] = r.Sum(p => p.Quantity / p.Ratio);
                }
                return ret;
            }
        }
    }
}
